import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Like } from 'typeorm';
import { Menus } from '../entity/menus.entity'
import { PageInit, PageNation } from 'src/interface/page.interface';
type T = any;
@Injectable()
export class MenusService {
    constructor(
        @InjectRepository(Menus)
        private menusRepository: Repository<Menus>,
    ) { }
    //创建角色
    async create(menu: Menus): Promise<Menus> {
        return await this.menusRepository.save(menu);
    }
    //更新菜单
    async update(menu: Menus): Promise<any> {
        try {
            // const one = await this.menusRepository.findOneBy({
            //     menu_id: menu.menu_id
            // })
            // console.log('输出one:', one)



        const res = await this.menusRepository.save(menu)
                console.log('输出更新后结果：', res)
                  return res;



        } catch (error) {
            throw new (error)
        }

    }
    async findAll(query: PageInit): Promise<PageNation> {
        //  console.log(query)
        const take = query.take || 10
        const skip = query.skip || 0
        const keyword = query.keyword || ''

        let res = await this.menusRepository.findAndCount({
            where: { menu_name: Like('%' + keyword + '%') }, order: { menu_name: "DESC" },
            take: take,
            skip: skip
        })

        const [result, total] = res;
        console.log('输出res:', result.length)
        return {
            list: result,
            count: total
        }
    }
}



